package com.linciping.plugin.element;

import javax.annotation.Nonnull;

public final class KtPropertyBuilder {
    private boolean isConst = false;
    private boolean isVar;
    private boolean isLateinit;
    private String name;
    private String type;
    private String init;
    private VisibilityEnum visibility;

    private KtPropertyBuilder() {
    }

    public static KtPropertyBuilder newBuilder() {
        return new KtPropertyBuilder();
    }

    public KtPropertyBuilder withIsConst(boolean isConst) {
        this.isConst = isConst;
        return this;
    }

    public KtPropertyBuilder withIsVar(boolean isVar) {
        this.isVar = isVar;
        return this;
    }

    public KtPropertyBuilder withIsLateinit(boolean isLateinit) {
        this.isLateinit = isLateinit;
        return this;
    }

    public KtPropertyBuilder withName(@Nonnull String name) {
        this.name = name;
        return this;
    }

    public KtPropertyBuilder withType(@Nonnull String type) {
        this.type = type;
        return this;
    }

    public KtPropertyBuilder withInit(String init) {
        this.init = init;
        return this;
    }

    public KtPropertyBuilder withVisibility(VisibilityEnum visibility) {
        this.visibility = visibility;
        return this;
    }

    public String build() {
        StringBuilder propertyContent = new StringBuilder("");
        if (visibility != null) {
            switch (visibility) {
                case PRIVATE:
                    propertyContent.append("private ");
                    break;
                case INTERNAL:
                    propertyContent.append("internal ");
                    break;
                case PROTECTED:
                    propertyContent.append("protect ");
                    break;
            }
        }
        if (isConst) {
            propertyContent.append("const ");
            if (isVar) {
                propertyContent.append("var ");
            } else {
                propertyContent.append("val ");
            }
        } else {
            if (isLateinit) {
                propertyContent.append("lateinit var ");
            } else {
                if (isVar) {
                    propertyContent.append("var ");
                } else {
                    propertyContent.append("val ");
                }
            }
        }
        propertyContent.append(name);
        if (isLateinit) {
            propertyContent.append(": ").append(type);
        } else if (init == null || init.isEmpty()) {
            propertyContent.append(": ").append(type).append("? = null");
        } else {
            propertyContent.append(" = ").append(init);
        }
        return propertyContent.toString();
    }
}